package com.happyrecyclingstation.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.happyrecyclingstation.dto.PosHrQueryDto;
import com.happyrecyclingstation.dto.PosHrSaveDto;
import com.happyrecyclingstation.dto.PosJobSeekerQueryDto;
import com.happyrecyclingstation.entity.Position;
import com.happyrecyclingstation.mapper.PositionMapper;
import com.happyrecyclingstation.mapper.UserMapper;
import com.happyrecyclingstation.service.PositionService;
import com.happyrecyclingstation.utils.CurrentHolder;
import com.happyrecyclingstation.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 首页职位列表
     * @return
     */
    @Override
    public List<JobSeekerPositionVO> getPositions() {
        return positionMapper.selectPosition();
    }

    /**
     * 求职者条件分页查询职位列表
     * @return
     */
    @Override
    public PageResult<JobSeekerPositionVO> getList(PosJobSeekerQueryDto posQueryParam) {
        PageHelper.startPage(posQueryParam.getCurrentPage(), posQueryParam.getPageSize());
        Page<JobSeekerPositionVO> page = positionMapper.selectList(posQueryParam);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * HR分页查询职位列表
     * @param hrQueryParam
     * @return
     */
    @Override
    public PageResult<HrPosVO> hrGetList(PosHrQueryDto hrQueryParam) {
        //根据hr的id查询关联的公司id
        Integer companyId = userMapper.selectCompanyId(CurrentHolder.getCurrentId());
        if (companyId == null){
            return new PageResult<>(0L, null);
        }
        hrQueryParam.setCompanyId(companyId);
        PageHelper.startPage(hrQueryParam.getCurrentPage(), hrQueryParam.getPageSize());
        Page<HrPosVO> page = positionMapper.hrSelectList(hrQueryParam);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * HR保存职位
     * @param posHrSaveDto
     */
    @Override
    public void savePosition(PosHrSaveDto posHrSaveDto) {
        //查询HR的公司id
        Integer companyId = userMapper.selectCompanyId(CurrentHolder.getCurrentId());
        Position position = new Position();
        //拷贝属性
        BeanUtils.copyProperties(posHrSaveDto, position);
        position.setCompanyId(companyId);
        position.setCreateId(CurrentHolder.getCurrentId());
        position.setStatus(1);
        position.setCreateTime(LocalDateTime.now());
        position.setUpdateTime(LocalDateTime.now());
        //新增职位
        positionMapper.insert(position);
    }

    /**
     * 管理员查询待审核的职位
     * @return
     */
    @Override
    public PageResult<AdminPosReviewVO> getPosByStatus(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<AdminPosReviewVO> page = positionMapper.selectPosByStatus();
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 根据职位id查询职位详情
     * @param id
     * @return
     */
    @Override
    public PosDetailVO getPosById(Integer id) {
        PosDetailVO posDetail = positionMapper.selectPosById(id);
        return posDetail;
    }

    /**
     * 管理员根据id审核通过职位
     * @param ids
     */
    @Override
    public void passReview(List<Integer> ids) {
        positionMapper.passReview(ids);
    }

    /**
     * 管理员拒绝职位审核
     * @param id
     * @param message
     */
    @Override
    public void rejectReview(Integer id, String message) {
        // 将职位状态改为审核不通过
        positionMapper.rejectReview(id);
        // TODO 暂未实现发送信息给HR审核不通过的原因，后续再做
    }

    /**
     * 管理员查询职位列表
     * @return
     */
    @Override
    public PageResult<HrPosVO> getAllPos(PosJobSeekerQueryDto posQueryParam) {
        PageHelper.startPage(posQueryParam.getCurrentPage(), posQueryParam.getPageSize());
        Page<HrPosVO> page = positionMapper.selectAllPos(posQueryParam);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * hr删除职位
     * @param ids
     */
    @Override
    public void deletePosition(List<Integer> ids) {
        positionMapper.deletePosition(ids);
    }

    /**
     * 根据职位id查询回显
     * @param id
     * @return
     */
    @Override
    public PosHrSaveVO getPosHrById(Integer id) {
        PosHrSaveVO posHrSaveVo = positionMapper.selectPosHrById(id);
        return posHrSaveVo;
    }

    /**
     * hr修改职位
     * @param posHrSaveDto
     */
    @Override
    public void updatePosition(PosHrSaveDto posHrSaveDto) {
        Position position = new Position();
        BeanUtils.copyProperties(posHrSaveDto, position);
        position.setStatus(1);
        position.setUpdateTime(LocalDateTime.now());
        positionMapper.updatePosition(position);
    }

    /**
     * 获取职位名称
     * @return
     */
    @Override
    public List<HrPosNameVO> getPosName() {
        // 查询hr关联的公司id，如果没有关联公司返回空数组
        Integer companyId = userMapper.isRelated(CurrentHolder.getCurrentId());
        if (companyId == null){
            return new ArrayList<>();
        }
        // 查询职位名称
        return positionMapper.selectPosNameByCompanyId(companyId);
    }
}
